import requests
# from lxml import html
import json
import time
import configparser


# # 测试 lxml
# def get_login_html():

#   r = requests.get("http://stock.bonree.com")
#   tree = html.fromstring(r.text)
#   form = tree.forms[0]
#   fields = form.fields

#   return fields["lt"], fields["_eventId"], fields["execution"]

# # 登录
# def login():

#   lt, eventid, execution = get_login_html()


# broker 券商对照表
_broket_list = {

  "1010": "东方证券",
  "1036": "财通证券",
  "1008": "兴业证券",
  "1022": "东兴证券",
  "1003": "中信建投",
  "1032": "中原证券",
  "1018": "长江证券",
  "1025": "国金证券",
  "1024": "光大证券",
  "1023": "方正证券",
  "1001": "中泰证券",
  "1040": "安信证券",
  "1030": "西部证券",
  "1028": "山西证券",
  "1014": "海通证券",
  "1013": "国泰君安",
  "1016": "国信证券",
  "1017": "平安证券",
  "1011": "广发证券",
  "1006": "华泰证券",
  "1012": "中国银河",
  "1009": "西南证券",
  "1020": "东北证券",
  "1096": "长城证券",
  "1019": "第一创业",
  "1015": "中信证券",
  "1021": "东吴证券",
  "1002": "九州证券",
  "1027": "华安证券",
  "1031": "浙商证券",
  "1026": "国元证券",
  "1029": "太平洋证券"
}

# 地区
_reqparam_beijin = json.dumps({"info":{"uuid":"f2368be2-71d8-9b9b-7c91-512a219db900","area":"1001","network":"1001","version":0,"broker":[1028,1020,1021,1010,1003,1015,1012,1024,1008,1006,1013,1040,1017,1011,1014,1096,1022,1029,1001,1023,1016,1031,1036,1009,1002,1032,1026,1030,1027,1019,1018,1025],"all_brokers":[1028,1020,1021,1010,1003,1015,1012,1024,1008,1006,1013,1040,1017,1011,1014,1096,1022,1029,1001,1023,1016,1031,1036,1009,1002,1032,1026,1030,1027,1019,1018,1025],"times":["09:30","10:00","13:00"]}})
_reqparam_shanghai = json.dumps({"info":{"uuid":"95cda1f8-d69c-613a-d977-215d51dc8c1c","area":"1002","network":"1001","version":0,"broker":[1028,1020,1021,1010,1003,1015,1012,1024,1008,1006,1013,1040,1017,1011,1014,1096,1022,1029,1001,1023,1016,1031,1036,1009,1002,1032,1026,1030,1027,1019,1018,1025],"all_brokers":[1028,1020,1021,1010,1003,1015,1012,1024,1008,1006,1013,1040,1017,1011,1014,1096,1022,1029,1001,1023,1016,1031,1036,1009,1002,1032,1026,1030,1027,1019,1018,1025],"times":["09:30","10:00","13:00"]}})
_reqparam_shenzhen = json.dumps({"info":{"uuid":"f7959642-2274-18ff-2098-2e894bf3aa35","area":"1003","network":"1001","version":0,"broker":[1028,1020,1021,1010,1003,1015,1012,1024,1008,1006,1013,1040,1017,1011,1014,1096,1022,1029,1001,1023,1016,1031,1036,1009,1002,1032,1026,1030,1027,1019,1018,1025],"all_brokers":[1028,1020,1021,1010,1003,1015,1012,1024,1008,1006,1013,1040,1017,1011,1014,1096,1022,1029,1001,1023,1016,1031,1036,1009,1002,1032,1026,1030,1027,1019,1018,1025],"times":["09:30","10:00","13:00"]}})


_cf = configparser.ConfigParser()
_cf.read("bonree.ini")


# 取登录后的页面数据
def _get_data(param, is_report_req):


  jar = requests.cookies.RequestsCookieJar()
  jar.set("JSESSIONID", _cf.get("cookie", "jsessionid"), domain = "stock.bonree.com", path = "/")
  jar.set("UM_distinctid", _cf.get("cookie", "um_distinctid"), domain = ".bonree.com", path = "/")
  jar.set("__guid", _cf.get("cookie", "guid"), domain = "stock.bonree.com", path = "/")
  jar.set("monitor_count", _cf.get("cookie", "monitor_count"), domain = "stock.bonree.com", path = "/")
  url = "http://stock.bonree.com/report/trade.do"
  payload = {"data": param}
  r = requests.post(url, cookies = jar, data = payload)

  # 处理结果数据
  return _deal_data(r.text, is_report_req)


def _getkey(item):
  return item["stedexAvg"]


def _cmp_stedexAvg(obj1, obj2):

  if(obj1["stedexAvg"] > obj2["stedexAvg"]):
    return 1
  elif(obj1["stedexAvg"] < obj2["stedexAvg"]):
    return -1
  else:
    return 0


def _deal_data(response, is_report_req):

  cj_index = 0

  try:
    cj_broker = 1018
    cj_index = 1
    cj_score = 0
    index = 0
    rtn = []
    
    # 长江分数 <= 592 分提示
    # 降序排序
    data = json.loads(response)
    datalist = data["info"]["data"]
    datalist.sort(key = _getkey, reverse = True)

    # 重新处理数据格式
    for obj in datalist:

      index = index + 1

      broker = obj["broker"]
      tmp_obj = {}
      tmp_obj["broker"] = broker
      tmp_obj["score"] = obj["stedexAvg"]
      tmp_obj["name"] = _broket_list[str(broker)]
      tmp_obj["rank"] = index
      rtn.append(tmp_obj)

      if(broker == cj_broker):
        cj_score = obj["stedexAvg"]
        cj_index = index

  except:
    cj_index = -1

  if(is_report_req):
    return rtn, cj_index
  else:
    return cj_score, cj_index


def cray_shanghai(is_report_req):
  return _get_data(_reqparam_shanghai, is_report_req)


def cray_shenzhen(is_report_req):
  return _get_data(_reqparam_shenzhen, is_report_req)


def cray_beijin(is_report_req):
  return _get_data(_reqparam_beijin, is_report_req)


if __name__ == "__main__":

  score_bj, rank_bj = _get_data(_reqparam_beijin, False)
  score_sh, rank_sh = _get_data(_reqparam_shanghai, False)
  score_sz, rank_sz = _get_data(_reqparam_shenzhen, False)
  print("beijin: %d, %d" % (score_bj, rank_bj))
  print("shanghai: %d, %d" % (score_sh, rank_sh))
  print("shenzhen: %d, %d" % (score_sz, rank_sz))

  # list_bj = _get_data(_reqparam_beijin, True)
  # list_bj = _get_data(_reqparam_shanghai, True)
  # list_bj = _get_data(_reqparam_shenzhen, True)
  # print(list_bj)